package com.mjk.manager.usercenter.beelog;


import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.base.work.BaseWork;
import com.mjk.common.base.work.annotation.BeeWork;
import com.mjk.common.core.jdbc.service.log.model.BeeLogModel;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.util.MapUtils;
import com.mjk.common.util.ObjectUtils;
import com.mjk.common.util.TimeUtils;
import com.mjk.manager.api.mapper.ApiMapper;
import com.mjk.manager.usercenter.sclogin.dto.LogDto;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 用于显示系统错误
 * @author Administrator
 *
 */
@BeeWork(work = "error")
public class BeeErrorWork extends BaseWork {
	@Autowired
	private BeeDBService beeDBService;
	@Autowired
	private ApiMapper mapper;
	
	public BeeResponse getErrorList(LogDto dto) {
		BeeQueryWrapper wrapper = new BeeQueryWrapper(new BeeLogModel());
		if(ObjectUtils.isNotEmpty(dto.getTraceId())) {
			wrapper.eq(BeeLogModel::getTranceId,dto.getTraceId());
		}
		if(ObjectUtils.isNotEmpty(dto.getLogWork())) {
			wrapper.eq(BeeLogModel::getWorkName, dto.getLogWork());
		}
		if(ObjectUtils.isNotEmpty(dto.getLogMethod())) {
			wrapper.eq(BeeLogModel::getMethodName, dto.getLogMethod());
		}
		if(ObjectUtils.isNotEmpty(dto.getItemType())) {
			wrapper.eq(BeeLogModel::getItemType,dto.getItemType());
		}
		Date startTime;
		Date endTime;
		if(ObjectUtils.isNotEmpty(dto.getStartTime())) {
			startTime = TimeUtils.parse(dto.getStartTime());
		}else {
//			dto.setStartTime(TimeUtils.formatTime(TimeUtils.getDayStart(new Date()), TimeUtils.DATE_FORMAT_DATETIME));
//			startTime = TimeUtils.getDayStart(new Date());
		}
		if(ObjectUtils.isNotEmpty(dto.getEndTime())) {
			endTime = TimeUtils.parse(dto.getEndTime());
		}else {
//			dto.setEndTime(TimeUtils.formatTime(TimeUtils.getDayEnd(new Date()), TimeUtils.DATE_FORMAT_DATETIME));
//			endTime = TimeUtils.getDayEnd(new Date());
		}
        //wrapper.between(BeeLogModel::getCreateTime,startTime,endTime);
		//生产时间段之内的数据 间隔1小时的数据
		
//		List<Map<String,Object>> logs = mapper.getErrorChart(dto);
		Map<String,Object> result = new HashMap<>();
		result.put("log", getInvervalTime(dto));
		if(isPage(dto)) {
			wrapper.setPage(dto.getPageStart(),dto.getPageSize());
			result.put("list", beeDBService.findManyByWrapperWithPage(wrapper));
		}else {
			result.put("list", beeDBService.findManyByWrapper(wrapper));
		}
		return BeeError.BEE_SUC.tranceError(result);
	}
	
	public  List<Map<String,Object>> getInvervalTime(LogDto dto) {
        String startTime = dto.getStartTime() + TimeUtils.START_DAY_TIME;
        String endTime = dto.getEndTime() + TimeUtils.END_DAY_TIME;
		Calendar c = Calendar.getInstance();
		c.setTime(TimeUtils.parse(startTime));
		Calendar c1 = Calendar.getInstance();
		c1.setTime(TimeUtils.parse(endTime));
		List<Map<String,Object>> logs = mapper.getErrorChart(dto);
		List<Map<String,Object>> invervalTime = new ArrayList<>();
		for(long i = c.getTimeInMillis(); i < c1.getTimeInMillis();i = i+60*60*1000 ) {
			Map<String,Object> item = new HashMap<>();
			c.setTimeInMillis(i);
			String time = TimeUtils.formatTime(c.getTime(), TimeUtils.DATE_FORMAT_DATEMIN);
			c.add(Calendar.MINUTE, 60);
			boolean isExit = false;
			for(Map<String,Object> log : logs) {
				 String value = MapUtils.getString(log, "RecordTime");
				 if(ObjectUtils.isNotEmpty(value) && value.equals(time)) {
					 isExit = true;
					 item.put("RecordTime", time);
					 item.put("num", MapUtils.getString(log, "num"));
				 }
			}
			if(!isExit) {
				item.put("RecordTime", time);
				 item.put("num", 0);
			}
			invervalTime.add(item);
		}
		return invervalTime;
	}
}
